const db = wx.cloud.database();
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 0,
        index2: 0,
        index3: 0,
        nianfen: ['请选择年份'],
        diqu: ['请选择地区'],
        danwei: ['请选择单位'],
        phone: 0,
        theList: ''
    },
    bindPickerChange: function (e) {
        let num = e.detail.value;
        let that = this;
        console.log('nianfen发送选择改变，携带值为', that.data.nianfen[num]);
        this.setData({
            index: e.detail.value
        })
        wx.request({ //发送请求
            url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42945&callback=?', //接收url
            data: {
                level: '2',
                grfiled: 'nianfen',
                grtext: that.data.nianfen[num]
            }, //接收参数
            success(res) {
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                }
                var list = [];
                for (var i in theJson.lists) { //循环写入下来框
                    list.push(theJson.lists[i].diqu)
                }
                that.setData({
                    diqu: list
                });
            }
        })
    },
    bindPickerChange2: function (e) {
        let num2 = e.detail.value;
        let that = this;
        console.log('diqu发送选择改变，携带值为', that.data.diqu[num2]);
        this.setData({
            index2: e.detail.value
        })
        wx.request({ //发送请求
            url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42945&callback=?', //接收url
            data: {
                level: '3',
                grfiled: 'diqu',
                grtext: that.data.diqu[num2],
                onefiled: 'nianfen',
                onetext: that.data.nianfen[this.data.index]
            }, //接收参数
            success(res) {
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                }
                console.log(theJson)
                var list = [];
                for (var i in theJson.lists) { //循环写入下来框
                    list.push(theJson.lists[i].danwei)

                }
                that.setData({
                    danwei: list
                });
            }
        })
    },
    bindPickerChange3: function (e) {
        let num3 = e.detail.value;
        let that = this;
        console.log('danwei发送选择改变，携带值为', that.data.danwei[num3]);
        this.setData({
            index3: e.detail.value
        })
    },

    onLoad: function (options) {
        var that = this;
        console.log(options);
        that.setData({
            area: options.area,
            geneal: options.agent
        })
        if (options.page) {
            that.setData({
                page: options.page
            })
        }

        wx.request({ //发送请求
            url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42945&callback=?', //接收url
            data: {
                level: '1',
                nianfen: ''
            }, //接收参数
            success(res) { //返回
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                }
                var list = [];
                for (var i in theJson.lists) { //循环写入下来框
                    list.push(theJson.lists[i].nianfen)
                }
                that.setData({
                    nianfen: list
                });
            }
        })
    },
    getPhoneNumber(e) {
        var that = this;
        var fenxiao = that.data.area;
        var geneal = that.data.geneal;
        wx.cloud.callFunction({
            name: 'getMobile',
            data: {
                weRunData: wx.cloud.CloudID(e.detail.cloudID),
            }
        }).then(res => {
            wx.setStorageSync('fsxphone', res.result)
            console.log(res.result)
            wx.request({ //发送请求
                url: 'https://zg99.offcn.com/index/chaxun/register?actid=42945&callback=?', //接收url
                data: {
                    phone: res.result,
                    fenxiao: fenxiao,
                    geneal: geneal,
                }, //接收参数
                success(res) {
                    var jsonStr = res.data;
                    jsonStr = jsonStr.replace("(", "");
                    jsonStr = jsonStr.replace(")", "");
                    if (typeof jsonStr != 'object') {
                        jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                        var theJson = JSON.parse(jsonStr);
                    }
                    console.log(theJson)
                    that.setData({
                        fixed: !1
                    })
                }
            })

            wx.request({
                url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
                method: 'POST',
                data: {
                    activeId: 23,
                    mobile: res.result,
                    areaCode: that.data.area,
                    agent: that.data.geneal,
                    pageId: that.data.page
                },
                success: res => {
                    console.log(res);
                }
            })
        }).catch(err => {
            console.error(err);
        });
    },



    tijiao: function (res) {
        var that = this;

        var phone = wx.getStorageSync('fsxphone')
        if (phone) {
            var nianfen = that.data.nianfen[that.data.index];
            var diqu = that.data.diqu[that.data.index2];
            var danwei = that.data.danwei[that.data.index3];
            wx.request({ //发送请求
                url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=42945&callback=?",
                data: {
                    nianfen: nianfen,
                    diqu: diqu,
                    danwei: danwei,
                    limits: '100'
                }, //接收参数
                success(res) { //返回
                    var jsonStr = res.data;
                    jsonStr = jsonStr.replace("(", "");
                    jsonStr = jsonStr.replace(")", "");
                    if (typeof jsonStr != 'object') {
                        jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                        var theJson = JSON.parse(jsonStr);
                    };
                    console.log(theJson)
                    that.setData({
                        theList: theJson.lists
                    })
                    console.log(that.data.theList)
                }
            })
        } else {
            wx.showToast({
                title: '请先登录',
                duration: 2000,
                icon: 'none'
            })
            that.setData({
                fixed: 1
            })
        }



    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})